
//格式时间==============生日===============例（2017-8-11）
function formatDate(date) {
    date = new Date(date * 1000);
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    return [year, month, day].map(formatNumber).join('-');
}

// TODO
function formatTime(date) {
    date = new Date(date);
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    return [year, month, day].map(formatNumber).join('-')+ ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
}

//反格式=================生日===================
/**
 * 时间
 */
function contraryFormatTime(time) {
    let that = this
    time = time + ' ' + ' 00:00:00 ';
    time = time.replace(new RegExp("-", "gm"), "/");
    return (new Date(time)).getTime() / 1000 // / 1000; //得到秒数
}


module.exports = {
    /**
     * 深度拷贝
     */
    deepClone: function (obj) {
        // 当有属性为undefine或null时将会被移除
        var newObj = JSON.parse(JSON.stringify(obj));
        return newObj;
    },

    /**
     * 交换数组元素位置
     */
    swapItem: function (array, index1, index2) {
        array[index1] = array.splice(index2, 1, array[index1])[0];
        return this;
    },

    /**
     * 去除指定字符串前后两端的空格
     */
    stringTrim: function (str) {
        if (typeof str != 'string') return;
        return str.replace(/(^\s*)|(\s*$)/g, "");
    },

    /**
     * 获取指定键值的子项
     */
    getItemIndexOfList: function (list, key, value) {
        if (key) {
            for (var i in list) {
                if (list[i][key] == value) {
                    return i;
                }
            }
        } else {
            for (var i in list) {
                if (list[i] == value) {
                    return i;
                }
            }
        }
        return -1
    },

    //格式时间
    formatTime: formatTime,
    formatDate: formatDate,

    //反格式时间
    contraryFormatTime: contraryFormatTime
}
